'use strict';

import $api from '@/api/api-cinema';
import $storage from "@/store/storage/storage";

// 数据字典-唯一KEY
export const DICT_KEY = 'DICT_CINEMA';

/**
 * 获取数据字典数据
 *
 * @returns {Promise<unknown>} 字典对象列表
 */
export const getData = () => $storage.getDictData(DICT_KEY)
  .then(data => Promise.resolve(data)).catch(() => refresh());

/**
 * 刷新数据字典数据
 *
 * @returns {Promise<unknown>} 字典对象列表
 */
export const refresh = () => {
  return $api.list().then(data => {
    const dictList = data.flatMap(obj => {
      return {
        id: obj.cinemaId,
        code: obj.cinemaCode,
        name: obj.cinemaName
      };
    });
    return $storage.refreshDictData(DICT_KEY, dictList);
  });
};

// 导出默认对象
export default {
  DICT_KEY,
  getData,
  refresh
};
